package com.jin.webvideo.websocket.shareroom.entity;

import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.websocket.shareroom.ShareRoom;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Map;

public class RoomUtil {
    public static boolean checkJoin(ShareRoom room, String account) {
        if (StringUtil.isEmpty(account) || room == null || room.isClose()){
            return false;
        }
        if (room.getMember().containsKey(account)){
            return false;
        }
        return true;
    }

    public static boolean isOwner(ShareRoom room, String account) {
        return account.equals(room.getRoomOwner());
    }

    public static boolean join(ShareRoom room, String account, WebSocketSession session){
        if (!RoomUtil.checkJoin(room, account)){
            return false;
        }
        if (RoomUtil.isOwner(room, account)){
            room.setStatus(ShareRoom.STATUS_OPEN);
        }
        room.putMember(account, session);
        if (room.getStatus() == ShareRoom.STATUS_NEW) {
            // send(session, ByteBuffer.allocate(0));
        }
        return true;
    }

    /**
     *
     * @param room
     * @param account
     * @return true 需要从manage中移除
     * @throws IOException
     */
    public static boolean leave(ShareRoom room, String account) throws IOException {
        WebSocketSession session = room.remove(account);
        if (!RoomUtil.isOwner(room, account)) {
            session.close();
            return false;
        }
        return true;
    }

    public static void close(ShareRoom room){
        if (room.isClose()){
            return;
        }
        Map<String, WebSocketSession> member = room.getMember();
        for (String account : member.keySet()){
            WebSocketSession session = member.get(account);
            if (null != session && session.isOpen()) {
                try {
                    session.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        room.setStatus(ShareRoom.STATUS_CLOSE);
    }


    public static void systemBroadcast(ShareRoom room, ByteBuffer byteBuffer){
        Map<String, WebSocketSession> member = room.getMember();
        for (String key : member.keySet()) {
            send( member.get(key), byteBuffer );
        }
    }

    public static void broadcast(ShareRoom room, String account, ByteBuffer byteBuffer){
        Map<String, WebSocketSession> member = room.getMember();
        for (String key : member.keySet()) {
            if (!key.equals(account)) {
                send( member.get(key), byteBuffer );
            }
        }
    }

    public static void send(WebSocketSession session, ByteBuffer byteBuffer) {
        if (session == null || !session.isOpen()){
            return;
        }
        try {
            session.sendMessage(new BinaryMessage(byteBuffer));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
